<?php   if ( ! defined('CentPHP')) exit( "Disallow to access direct!" );
/*
    |====================================================|
	|  CentPHP                                           |
	|----------------------------------------------------|
	|  package      lib/kernel/Class.Mysql.php                |
	|  auther       Boyu Sun                             |
	|  copyright    Copyright (c) 2008, sunboyu.cn       |
	|  license      freebsd-license                      |
	|  link         www.sunboyu.cn                       |
	|  version      Version 0.1                          |
	|====================================================|
*/

/*
    +-------------------------------------------------------------------
	| class Mysql
	| Mysql driver.
	+-------------------------------------------------------------------
*/

interface DateBaseConnect
{
    public function Connect( );
	public function selectDateBase( );
	public function query( $sql );
	public function fetch( $sql );
	public function fetchAll( $sql );
	public function affectedRow();
	public function recordCount();
	public function insertID();
	public function close();
}

class Mysql implements DateBaseConnect
{
	public $handle = false;
	public $query;
	public $exetime;
	public $host;
	public $user;
	public $pass;
	public $datebase;


	/*
	 *  Constructor
	 */
    function Mysql()
	{
		//
	}
	/*
	 *  Initialize
	 *  @param array()
	 *  @retrun null
	 */
	function _init( $config = array() )
	{
		foreach( $config as $key => $value )
		{
			$this->$key = $value;
		}
		$this->Connect();
	}
	/*
	 *  Connetc to the datebase
	 *  @param null
	 *  @retrun null
	 */
    public function Connect( )
	{
		$this->handle = @mysql_connect( $this->host , $this->user , $this->pass , false ) or die("Can't connetc to the DateBse.".mysql_error());
		$this->selectDateBase( $this->datebase );
		$this->exetime = 0;
	}
	/*
	 *  Select a datebase
	 *  @param null
	 *  @retrun null
	 */
	public function selectDateBase( )
	{
		@mysql_select_db( $this->datebase , $this->handle ) or die("Can't select the DateBase".mysql_error());
	}
	/*
	 *  Execute a sql
	 *  @param null
	 *  @retrun null
	 */
	public function query( $sql )
	{
		$this->query = mysql_query( $sql , $this->handle ) or die("query error".mysql_error());
		$this->exetime++;
		return true;
	}
	/*
	 *  Fetch the first row of the result
	 *  @param null
	 *  @retrun null
	 */
	public function fetch( $sql )
	{
		$this->query( $sql );
		while( $row = mysql_fetch_array( $this->query, MYSQL_ASSOC ) )
		{
			return $row;
	    }
		return false;
	}
	/*
	 *  Fetch all of the result
	 *  @param null
	 *  @retrun null
	 */
	public function fetchAll( $sql )
	{
		$this->query( $sql );
		$rows = false;
		while( $rs = mysql_fetch_array( $this->query, MYSQL_ASSOC ) )
		{
			$rows[] = $rs;
		}
		return $rows;
	}
	/*
	 *  Get the number of affect row
	 *  @param null
	 *  @retrun null
	 */
	public function affectedRow()
	{
		return mysql_affected_rows( $this->query );
	}
	/*
	 *  Get the number of result
	 *  @param null
	 *  @retrun null
	 */
	public function recordCount()
	{
		return mysql_num_rows( $this->query );
	}
	/*
	 *  Get the last id of row inserted
	 *  @param null
	 *  @retrun null
	 */
	public function insertID()
	{
		return (mysql_insert_id()>0) ? mysql_insert_id() : false;
	}
	/*
	 *  Free the connect
	 *  @param null
	 *  @retrun null
	 */
	public function close()
	{
		@mysql_close( $this->handle );
	}
    /*
	 *  Insert a recorder by array
	 *  @param null
	 *  @retrun null
	 */
	function _insert( $array , $table )
	{
	    $sql = "INSERT INTO {$table} (";
	    if(!is_array($array))
		{
		    die('the array is not a array!');
		}
		$keys = array_keys( $array );
		$sql .= implode( ',' , $keys );
		$sql .= ") VALUES (";
		foreach( $array as $key=>$value )
		{
		    if(is_int( $array[$key] ))
			{
			    $sql .= $array[$key];
			}
			else if(is_string( $array[$key] ))
			{
			    $sql .= "'{$array[$key]}'";
			}
			$sql .= ",";
		}
		$sql = substr_replace( $sql , "" , -1 );
		$sql .= ")";
		return $this->query( $sql );
	}
	 /*
	 *  Destructer
	 *  @param null
	 *  @retrun null
	 */
	function __destruct()
	{
		//$this->close();
	}
}

?>